<?php
/**
 * ActionColumn 继承.
 * User: ZFC
 * Date: 2015/7/6
 * Time: 18:42
 */

namespace common\widgets;

use Yii;
use yii\helpers\Html;
use yii\helpers\Url;
use Closure;

class ActionColumn extends \yii\grid\ActionColumn {

    public $header = "操作";

    public $template = '{view} {edit} {delete}';

    /**
     * 默认按钮初始化.
     */
    protected function initDefaultButtons()
    {
        $this->buttonOptions['class'] = 'btn btn-default btn-xs btn-ajax-modal';
        if (!isset($this->buttons['view'])) {
            $this->buttons['view'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', '查看'),
                    'aria-label' => Yii::t('yii', 'View'),
                    'data-pjax' => '1',
                    'data-target' => '#viewModal',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-eye-open"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['edit'])) {
            $this->buttons['edit'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', '编辑'),
                    'aria-label' => Yii::t('yii', 'Edit'),
                    'data-pjax' => '1',
                    'data-target' => '#editModal',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-pencil"></span>', $url, $options);
            };
        }
        if (!isset($this->buttons['delete'])) {
            $this->buttons['delete'] = function ($url, $model, $key) {
                $options = array_merge([
                    'title' => Yii::t('yii', '删除'),
                    'aria-label' => Yii::t('yii', 'Delete'),
                    'data-confirm' => Yii::t('yii', '确定要删除此项？'),
                    'data-method' => 'post',
                    'data-pjax' => '1',
                    'data-target' => '#deleteModal',
                    'href' => 'javascript:void(0);',
                ], $this->buttonOptions);
                return Html::a('<span class="glyphicon glyphicon-trash"></span>', $url, $options);
            };
        }
    }


    public function createUrl($action, $model, $key, $index)
    {
        if ($this->urlCreator instanceof Closure) {
            return call_user_func($this->urlCreator, $action, $model, $key, $index);
        } else {
            $params = is_array($key) ? $key : ['id' => (string) $key];
            $params[0] = $this->controller ? $this->controller . '/' . $action : $action;

            return Url::toRoute($params);
        }
    }

    /**
     * @inheritdoc
     */
    protected function renderDataCellContent($model, $key, $index)
    {
        return preg_replace_callback('/\\{([\w\-\/]+)\\}/', function ($matches) use ($model, $key, $index) {
            $name = $matches[1];
            if (isset($this->buttons[$name])) {
                $url = $this->createUrl($name, $model, $key, $index);

                return call_user_func($this->buttons[$name], $url, $model, $key);
            } else {
                return '';
            }
        }, $this->template);
    }

}